﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
public partial class UserManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //添加修改
    protected void btnOK_Click(object sender, EventArgs e)
    {
        add();
    }
   

    //添加
    private void add()
    {
        if (!isEmpty() == false)
        {
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                Users user = lqDB.Users.FirstOrDefault(u => u.UserName == txtName.Text);
                if (user == null)
                {
                    Users newuser = new Users();
                    newuser.UserName = txtName.Text;
                    newuser.LoginName = txtLoginname.Text;
                    newuser.PassWord = txtPsw.Text;
                    newuser.Phone = txtPhone.Text;
                    newuser.Communities = cmdCommunity.SelectedItem.Value.ToString();
                    lqDB.Users.InsertOnSubmit(newuser);
                    lqDB.SubmitChanges();
                    Frame.Global.GlobalFunctions.AlertToUrl("添加成功！", "EditUser.aspx");
                }
                else
                {
                    Frame.Global.GlobalFunctions.Alert("对不起，此用户已经存在！");
                }

            }
        }
    }
    //判断是否为空
    private bool isEmpty()
    {
        bool isempty = true;
        if (txtName.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("用户名不能为空！");
            isempty = false;
        }
        else if (txtLoginname.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("登录名不能为空！");
            isempty = false;
        }
        else if (cmdCommunity.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择社区！");
            isempty = false;
        }
        else if (txtPsw.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("密码不能为空！");
            isempty = false;
        }
        else if (txtPhone.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("电话号码不能为空！");
            isempty = false;
        }
        else
        {
            isempty = true;
        }
        return isempty;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditUser.aspx");
    }
}